﻿var express = require('express');
var Fiber = require('fibers');
var bodyParser = require('body-parser');
var orm = require('./modules/orm.js');

var Sex = orm.defineModel('sex', {
    fields: {
        name: orm.fields.Char()
    }
});

var UserType = orm.defineModel('usertype', {
    fields: {
        name: orm.fields.Char(),
        description: orm.fields.Char()
    }
});

var User = orm.defineModel('user', {
    fields: {
        name: orm.fields.Char(),
        address: orm.fields.Char(),
        age: orm.fields.Integer(),
        phone: orm.fields.Char(),
        type: orm.fields.Reference({
            target: 'usertype',
            type: 'n1'
        }),
        password: orm.fields.Char(),
        sex: orm.fields.Reference({
            target: 'sex',
            type: 'n1'
        })
    }
});

var Caregiver = orm.defineModel('caregiver', {
    fields: {
        user: orm.fields.Reference({
            target: 'user',
            type: 'n1'
        }),
        skills: orm.fields.Char(),
        working_expierences: orm.fields.Char(),
        working_years: orm.fields.Integer(),
        resume: orm.fields.Char(),
        time_schedule: orm.fields.Char(),
        training_record: orm.fields.Char(),
        photo_url: orm.fields.Char(),
        need_salary: orm.fields.Integer(),
        edu_bkgd: orm.fields.Integer()
    }
});

var sessionFactory = new orm.SessionFactory({
        host: '127.0.0.1',
        user: 'root',
        password: 'dataserver',
        database: 'carepro'
});

var app = express();

app.use(express.static(__dirname + '/static'));
app.use(bodyParser.json());

app.post('/user/find', function(req, res) {
    var fn = Fiber(function() {
        var session = sessionFactory.createSession();
        session.begin();

        // 参数存放在req.body中，已经由中间件将其转换为JavaScript对象
        var user = new User(req.body.user);

        var foundModel = session.getOne(user, {
            recurisive: true
        });

        session.end();
        res.json(foundModel);
    });

    fn.run();
});

app.post('/caregiver/search', function(req, res) {
    var fn = Fiber(function() {
        var session = sessionFactory.createSession();
        session.begin();

        var foundModels = session.getAll(Caregiver, {
            recurisive: true
        });

        if ( 'filters' in req.body ) {
            var filters = req.body.filters;
            if ( 'age' in filters ) {
                foundModels = foundModels.filter(function(model) {
                    return model.user.age >= filters.age.min && 
                        model.user.age < filters.age.max;
                });
            }
            if ( 'workingYears' in filters ) {
                foundModels = foundModels.filter(function(model) {
                    return model.working_years >= filters.workingYears.min && 
                        model.working_years < filters.workingYears.max;
                });
            }
            if ( 'educationBackground' in filters ) {
                foundModels = foundModels.filter(function(model) {
                    return model.edu_bkgd == filters.educationBackground;
                });
            }
        }

        session.end();
        res.json({
            data: {
                caregivers: foundModels
            },
            successful: true
        });
    });

    fn.run();
});

var server = app.listen(3000, function() {
    console.log('Listening on port %d', server.address().port);
});
